package io.renren.modules.job.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.util.DigestUtils;

import java.io.IOException;

/**
 * @author csl
 * @create 2020-10-21 14:26
 */
public class messageUtil {
    public static boolean sendemil(String username,String phone,String mail ,String subject, String content) {
        HttpClient httpClient = new DefaultHttpClient();

        // api请求地址url
        String url = "http://apis.cdivtc.com/mp_message_pocket_web-mp-restful-message-send/ProxyService/message_pocket_web-mp-restful-message-sendProxyService";
        String appid = "c6f0f319db469887";
        String accessToken = "3e5b57f3957d534f292b44dc45b2ce1a";
        String schoolCode = "5657";
        String sign = DigestUtils.md5DigestAsHex((accessToken + "" + schoolCode + "" + username).getBytes());
        // 业务参数
        String paramJson = "{"
                + "\"appId\": \"" + appid + "\","
                + "    \"subject\": \"" + subject + "\","
                + "    \"content\": \"" + content.replace(" ", "").replace("\r\n","") + "\","
//                +"\"mobileUrl\": \"http://www.baidu.com\","
                +"    \"sendType\":12345,"
                        + "    \"wxSendType\":\"text\","
                        + "    \"sendNow\": true,"
                        + "   \"tagId\": 1012,"
                        + "    \"receivers\": ["
                        + "        {"
                        + "            \"userId\": \"" + username + "\","
                        + "            \"mobile\": \"" + phone + "\","
                        + "            \"email\": \"" + mail + "\","
                        + "            \"flag\": 0"
                        + "        }"
                        + "    ],"
                        + "    \"schoolCode\": \"5657\","
                        + "    \"sign\": \"" + sign + "\""
                        + "}\"";
        // 解决中文乱码问题
        StringEntity entity = new StringEntity(paramJson, "utf-8");
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");

        // 获取调用方法
        HttpPost method = new HttpPost(url);
        method.setEntity(entity);
        // appId accessToken Header头文件信息传入
        method.setHeader("appId", appid);
        method.setHeader("accessToken", accessToken);

        // 方法调用
        HttpResponse result = null;
        try {
            result = httpClient.execute(method);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 解析结果
        HttpEntity ret = result.getEntity();
        String retStr = null;
        try {
            retStr = EntityUtils.toString(ret);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(retStr);
        String code = JSONObject.parseObject(retStr).getString("msg");

        return true;
     //   return code.equals("success") ? true : false;
    }



}
